<html>
<head><meta charset="utf-8"><title>bindings_after_at · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html">bindings_after_at</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211055431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211055431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211055431">(Sep 23 2020 at 20:05)</a>:</h4>
<p>Are there any plans to allow binding <code>Copy</code> fields of non-<code>Copy</code> types after an <code>@</code> in a pattern?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(bindings_after_at)]</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">NonCopy</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">copy_field</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">example</span><span class="p">(</span><span class="n">nc</span>: <span class="nc">NonCopy</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">nc</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">nc2</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">NonCopy</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">copy_field</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<div class="codehilite"><pre><span></span><code>error[E0007]: cannot bind by-move with sub-bindings
 --&gt; src/lib.rs:9:9
  |
9 |         nc2 @ NonCopy { copy_field } =&gt; {}
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ binds an already bound by-move value by moving it

error[E0382]: use of moved value: `nc`
 --&gt; src/lib.rs:9:25
  |
7 | fn example(nc: NonCopy) {
  |            -- move occurs because `nc` has type `NonCopy`, which does not implement the `Copy` trait
8 |     match nc {
9 |         nc2 @ NonCopy { copy_field } =&gt; {}
  |         ----------------^^^^^^^^^^--
  |         |               |
  |         |               value used here after move
  |         value moved here
</code></pre></div>



<a name="211055572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211055572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211055572">(Sep 23 2020 at 20:06)</a>:</h4>
<p>On first glance, this seems like it should be fine as all the bindings are <code>Copy</code>, so they can be copied and destructured, while the original value moves to <code>nc2</code>.</p>



<a name="211056427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211056427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211056427">(Sep 23 2020 at 20:14)</a>:</h4>
<p>At first glace that seems like it could be one of those "things that AST borrowck blocked did because doing anything else was harder", so you might want to explore just bypassing the check and seeing what MIR borrowck does with it.  If it already works (and MIRI is happy with it) then making it allowed seems very plausible...</p>



<a name="211059284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211059284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211059284">(Sep 23 2020 at 20:40)</a>:</h4>
<p>+1 from me, I've tripped on this before</p>



<a name="211125334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211125334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211125334">(Sep 24 2020 at 12:44)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> so, at a high level, find the code that generates the warning, disable it, build the compiler, then compile my example above. If it compiles, rebuild Miri against that compilers code and run the example in Miri?</p>



<a name="211178197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/bindings_after_at/near/211178197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/bindings_after_at.html#211178197">(Sep 24 2020 at 19:23)</a>:</h4>
<p>Hmm, that sounds like more of a pain than I hoped.  The MIRI part was just that I'm not sure how the code is written to read out the field -- if it's matched against a <code>&amp;mut</code> we'd of course need to make sure it either read out the field before making the <code>&amp;mut</code> from the <code>@</code>, or to read the copy field through the new <code>&amp;mut</code>.  I don't know if that happens today.  (And maybe MIR borrowck finds it fine and we wouldn't need the MIRI UB check to find it.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>